//
//  MarketingThreeCell.m
//  EMMApp
//
//  Created by Mac on 16/8/19.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "MarketingThreeCell.h"
#import "HousingCVCell.h"

@interface MarketingThreeCell () {
    NSInteger num;
}
@property (nonatomic, strong) NSMutableArray *roomList;
@property (nonatomic, strong) UIScrollView  *scrollView;

@end

@implementation MarketingThreeCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}
- (void)initWithNSMutableArray:(NSMutableArray *)mArr indexNum:(NSInteger)indexNum {
    _roomList = mArr;
    
//    for (RoomList *lr in mArr) {
//        [_roomList addObject:lr];
//    }
//    num = indexNum;
    
    _scrollView = [[UIScrollView  alloc] initWithFrame:CGRectMake(0, 41, EMM_WIDTH, 70)];
//    [self addSubview:_scrollView];
    //这个属性很重要，它可以决定是横向还是纵向滚动，一般来说也是其中的 View 的总宽度，和总的高度
    //这里同时考虑到每个 View 间的空隙，所以宽度是 200x3＋5＋10＋10＋5＝630 emm-20/3*count
    //高度上与 ScrollView 相同，只在横向扩展，所以只要在横向上滚动
    float h = [_roomList[num] count]/3.00;
    _scrollView.contentSize = CGSizeMake(EMM_WIDTH*ceilf(h), 70);
    
    //用它指定 ScrollView 中内容的当前位置，即相对于 ScrollView 的左上顶点的偏移
    _scrollView.contentOffset = CGPointMake(0, 0);
    //按页滚动，总是一次一个宽度，或一个高度单位的滚动
    //滑动时是否显示滑动条
    _scrollView.showsHorizontalScrollIndicator = YES;
    _scrollView.pagingEnabled =  YES;
//    [self creatCollectionView];

}


#pragma mark - UICollectionView初始化
- (void)creatCollectionView {
    
    UICollectionViewFlowLayout *layuet = [[UICollectionViewFlowLayout alloc] init];
    //垂直行间距
    layuet.minimumLineSpacing = 1;
    //水平间隔
    layuet.minimumInteritemSpacing = 1;
    
    [layuet setScrollDirection:UICollectionViewScrollDirectionVertical];
    float h = [_roomList count]/3.00;
    self.homeCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH*ceilf(h), 70) collectionViewLayout:layuet];
    [self.homeCollectionView registerNib:[UINib nibWithNibName:@"HousingCVCell" bundle:nil] forCellWithReuseIdentifier:@"HousingCVCell"];
    self.homeCollectionView.delegate = self;
    self.homeCollectionView.dataSource = self;
    self.homeCollectionView.backgroundColor = [UIColor colorFromHexCode:@"f6fafb"];
    self.homeCollectionView.scrollEnabled = NO;
    [self.homeCollectionView reloadData];
    [self addSubview:self.homeCollectionView];

}

#pragma mark - UICollectionViewDataSource / UICollectionViewDelegate /UICollectionViewDelegateFlowLayout 代理
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return [_roomList count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"HousingCVCell";
    HousingCVCell *homeCell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    homeCell.backgroundColor = [UIColor colorFromHexCode:@"f6fafb"];
//    homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#999999"];
    
        RoomList *list = [_roomList objectAtIndex:indexPath.row];
        homeCell.titleLabel.layer.masksToBounds = YES;
        homeCell.titleLabel.layer.cornerRadius = 4;
        homeCell.titleLabel.text = list.roomName;
        if ([list.roomStatus isEqualToString:@"销控"]) {
            homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#999999"];
        }else if ([list.roomStatus isEqualToString:@"可售"]) {
            homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#6ac96b"];
        }else if ([list.roomStatus isEqualToString:@"小订"]) {
            homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#ff6600"];
        }else if ([list.roomStatus isEqualToString:@"认购"]) {
            homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#ffcc00"];
        }else if ([list.roomStatus isEqualToString:@"签约"]) {
            homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#f33e47"];
        }else if ([list.roomStatus isEqualToString:@"保留"]) {
            homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#66cdcc"];
        }else if ([list.roomStatus isEqualToString:@"暂不可售"]) {
            homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#3b3b3b"];
        }
    return homeCell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((EMM_WIDTH-20)/3, 60);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //    RoomList *list = [_roomList objectAtIndex:indexPath.row];
    //    HousingDetailsViewController *evc = [[HousingDetailsViewController alloc] init];
    //    evc.roomId = list.roomId;
    //    [self.navigationController pushViewController:evc animated:YES];
//    if (self.delegate!=nil &&[self.delegate respondsToSelector:@selector(marketingTwoCelldidSelectedTableview:withcollecttion:)]) {
//        [self.delegate marketingTwoCelldidSelectedTableview:num withcollecttion:indexPath];
//    }
}
@end
